
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Nerves · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../ecto/basics.html" />
    
    
    <link rel="prev" href="protocols.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        <li class="header">第一部分：基础</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../basics/basics.html">
            
                <a href="../basics/basics.html">
            
                    
                    基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../basics/collections.html">
            
                <a href="../basics/collections.html">
            
                    
                    集合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../basics/enum.html">
            
                <a href="../basics/enum.html">
            
                    
                    Enum 模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../basics/pattern-matching.html">
            
                <a href="../basics/pattern-matching.html">
            
                    
                    模式匹配
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../basics/control-structures.html">
            
                <a href="../basics/control-structures.html">
            
                    
                    控制语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../basics/functions.html">
            
                <a href="../basics/functions.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../basics/pipe-operator.html">
            
                <a href="../basics/pipe-operator.html">
            
                    
                    管道操作符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../basics/modules.html">
            
                <a href="../basics/modules.html">
            
                    
                    模块(Module)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../basics/mix.html">
            
                <a href="../basics/mix.html">
            
                    
                    Mix
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../basics/sigils.html">
            
                <a href="../basics/sigils.html">
            
                    
                    魔符(Sigil)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../basics/documentation.html">
            
                <a href="../basics/documentation.html">
            
                    
                    文档模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="../basics/testing.html">
            
                <a href="../basics/testing.html">
            
                    
                    测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="../basics/comprehensions.html">
            
                <a href="../basics/comprehensions.html">
            
                    
                    推导
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="../basics/strings.html">
            
                <a href="../basics/strings.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="../basics/date-time.html">
            
                <a href="../basics/date-time.html">
            
                    
                    日期和时间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="../basics/mix-tasks.html">
            
                <a href="../basics/mix-tasks.html">
            
                    
                    自定义Mix任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="../basics/iex-helpers.html">
            
                <a href="../basics/iex-helpers.html">
            
                    
                    IEx辅助函数
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第二部分：高级</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="erlang.html">
            
                <a href="erlang.html">
            
                    
                    和Erlang互操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="error-handling.html">
            
                <a href="error-handling.html">
            
                    
                    错误处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="escripts.html">
            
                <a href="escripts.html">
            
                    
                    可执行文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="concurrency.html">
            
                <a href="concurrency.html">
            
                    
                    并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="otp-concurrency.html">
            
                <a href="otp-concurrency.html">
            
                    
                    OTP并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="otp-supervisors.html">
            
                <a href="otp-supervisors.html">
            
                    
                    OTP Supervisors
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="otp-distribution.html">
            
                <a href="otp-distribution.html">
            
                    
                    OTP 分布式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="metaprogramming.html">
            
                <a href="metaprogramming.html">
            
                    
                    元编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.9" data-path="umbrella-projects.html">
            
                <a href="umbrella-projects.html">
            
                    
                    Umbrella Projects
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.10" data-path="typespec.html">
            
                <a href="typespec.html">
            
                    
                    Specifications and types
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.11" data-path="behaviours.html">
            
                <a href="behaviours.html">
            
                    
                    行为
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.12" data-path="gen-stage.html">
            
                <a href="gen-stage.html">
            
                    
                    GenStage
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.13" data-path="protocols.html">
            
                <a href="protocols.html">
            
                    
                    协议
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="2.14" data-path="nerves.html">
            
                <a href="nerves.html">
            
                    
                    Nerves
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第三部分：ECTO</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../ecto/basics.html">
            
                <a href="../ecto/basics.html">
            
                    
                    Basics
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="../ecto/changesets.html">
            
                <a href="../ecto/changesets.html">
            
                    
                    Changesets
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="../ecto/associations.html">
            
                <a href="../ecto/associations.html">
            
                    
                    关联关系
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="../ecto/querying.html">
            
                <a href="../ecto/querying.html">
            
                    
                    查询
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第四部分：专题</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../specifics/plug.html">
            
                <a href="../specifics/plug.html">
            
                    
                    Plug
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="../specifics/eex.html">
            
                <a href="../specifics/eex.html">
            
                    
                    嵌入的 Elixir(EEx)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="../specifics/ets.html">
            
                <a href="../specifics/ets.html">
            
                    
                    Erlang 项式存储(ETS)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="../specifics/mnesia.html">
            
                <a href="../specifics/mnesia.html">
            
                    
                    Mnesia 数据库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="../specifics/debugging.html">
            
                <a href="../specifics/debugging.html">
            
                    
                    调试
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第五部分：程序库</li>
        
        
    
        <li class="chapter " data-level="5.1" data-path="../libraries/guardian.html">
            
                <a href="../libraries/guardian.html">
            
                    
                    Guardian(基础)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="../libraries/poolboy.html">
            
                <a href="../libraries/poolboy.html">
            
                    
                    Poolboy
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="../libraries/benchee.html">
            
                <a href="../libraries/benchee.html">
            
                    
                    Benchee
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="../libraries/bypass.html">
            
                <a href="../libraries/bypass.html">
            
                    
                    Bypass
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="../libraries/distillery.html">
            
                <a href="../libraries/distillery.html">
            
                    
                    Distillery(基础)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="../libraries/stream-data.html">
            
                <a href="../libraries/stream-data.html">
            
                    
                    StreamData
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >Nerves</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h2 id="nerves-&#x7B80;&#x4ECB;">Nerves &#x7B80;&#x4ECB;</h2>
<p>&#x6211;&#x4EEC;&#x5C06;&#x5728;&#x672C;&#x8BFE;&#x4E2D;&#x4ECB;&#x7ECD; Nerves&#xFF0C; Nerves &#x9879;&#x76EE;&#x662F;&#x5728;&#x5D4C;&#x5165;&#x5F0F;&#x8F6F;&#x4EF6;&#x5F00;&#x53D1;&#x4E2D;&#x4F7F;&#x7528; Elixir &#x7684;&#x6846;&#x67B6;&#x3002; &#x6B63;&#x5982; Nerves &#x5B98;&#x7F51;&#x6240;&#x8BF4;&#xFF0C;&#x5B83;&#x5141;&#x8BB8;&#x60A8;&#x201C;&#x4F7F;&#x7528; Elixir &#x5236;&#x4F5C;&#x548C;&#x90E8;&#x7F72;&#x9632;&#x5F39;&#x5D4C;&#x5165;&#x5F0F;&#x8F6F;&#x4EF6;&#x201D;&#x3002; &#x672C;&#x8BFE;&#x5C06;&#x4E0E;&#x5176;&#x4ED6; Elixir School&#x8BFE;&#x7A0B;&#x7565;&#x6709;&#x4E0D;&#x540C;&#x3002; Nerves &#x5B66;&#x4E60;&#x8D77;&#x6765;&#x6709;&#x70B9;&#x56F0;&#x96BE;&#xFF0C;&#x56E0;&#x4E3A;&#x5B83;&#x9700;&#x8981;&#x4E00;&#x4E9B;&#x5148;&#x8FDB;&#x7684;&#x7CFB;&#x7EDF;&#x8BBE;&#x7F6E;&#x548C;&#x989D;&#x5916;&#x7684;&#x786C;&#x4EF6;&#xFF0C;&#x56E0;&#x6B64;&#x53EF;&#x80FD;&#x4E0D;&#x9002;&#x5408;&#x521D;&#x5B66;&#x8005;&#x3002;</p>
<p>&#x8981;&#x4F7F;&#x7528; Nerves &#x7F16;&#x5199;&#x5D4C;&#x5165;&#x5F0F;&#x4EE3;&#x7801;&#xFF0C;&#x60A8;&#x9700;&#x8981;&#x4E00;&#x4E2A;<a href="https://hexdocs.pm/nerves/targets.html" target="_blank">&#x76EE;&#x6807;&#x8BBE;&#x5907;</a>&#xFF0C;&#x4E00;&#x4E2A;&#x5E26;&#x6709;&#x60A8;&#x6240;&#x9009;&#x786C;&#x4EF6;&#x652F;&#x6301;&#x7684;&#x5B58;&#x50A8;&#x5361;&#x7684;&#x8BFB;&#x5361;&#x5668;&#xFF0C;&#x4EE5;&#x53CA;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x7F51;&#x7EDC;&#x8BBF;&#x95EE;&#x6B64;&#x8BBE;&#x5907;&#x3002;</p>
<p>&#x5728;&#x8FD9;&#x91CC;&#x6211;&#x4EEC;&#x5EFA;&#x8BAE;&#x4F7F;&#x7528; Raspberry Pi (&#x6811;&#x8393;&#x6D3E;)&#xFF0C;&#x56E0;&#x4E3A;&#x5B83;&#x5177;&#x6709;&#x53EF;&#x63A7;&#x7684; LED &#x677F;&#x3002; &#x540C;&#x65F6;&#x4E5F;&#x5EFA;&#x8BAE;&#x5C06;&#x76EE;&#x6807;&#x8BBE;&#x5907;&#x8FDE;&#x63A5;&#x5230;&#x4E00;&#x4E2A;&#x5C4F;&#x5E55;&#xFF0C;&#x56E0;&#x4E3A;&#x8FD9;&#x6837;&#x53EF;&#x4EE5;&#x7B80;&#x5316;&#x4F7F;&#x7528; IEx &#x7684;&#x8C03;&#x8BD5;&#x3002;</p>
<h2 id="&#x73AF;&#x5883;&#x5B89;&#x88C5;">&#x73AF;&#x5883;&#x5B89;&#x88C5;</h2>
<p>Nerves &#x9879;&#x76EE;&#x672C;&#x8EAB;&#x6709;&#x4E00;&#x4E2A;&#x5F88;&#x597D;&#x7684;<a href="https://hexdocs.pm/nerves/getting-started.html" target="_blank">&#x5165;&#x95E8;&#x6307;&#x5357;</a>&#xFF0C;&#x4F46;&#x6587;&#x6863;&#x91CC;&#x9762;&#x5927;&#x91CF;&#x7684;&#x7EC6;&#x8282;&#x5BF9;&#x4E8E;&#x4E00;&#x4E9B;&#x7528;&#x6237;&#x6765;&#x8BF4;&#x53EF;&#x80FD;&#x6709;&#x70B9;&#x538B;&#x529B;&#x3002; &#x76F8;&#x53CD;&#x5730;&#xFF0C;&#x672C;&#x6559;&#x7A0B;&#x5C06;&#x5C1D;&#x8BD5;&#x5448;&#x73B0;&#x201C;&#x5C11;&#x8BF4;&#x8BDD;&#xFF0C;&#x653E;&#x7801;&#x8FC7;&#x6765;&#x201D;&#x3002;</p>
<p>&#x9996;&#x5148;&#xFF0C;&#x60A8;&#x9700;&#x8981;&#x5B89;&#x88C5;&#x73AF;&#x5883;&#x3002; &#x60A8;&#x53EF;&#x4EE5;&#x5728; Nerves wiki &#x7684; <a href="https://hexdocs.pm/nerves/installation.html" target="_blank">&#x5B89;&#x88C5;</a> &#x90E8;&#x5206;&#x627E;&#x5230;&#x8BE5;&#x6307;&#x5357;&#x3002; &#x8BF7;&#x786E;&#x4FDD;&#x60A8;&#x540C;&#x65F6;&#x4E5F;&#x5DF2;&#x7ECF;&#x5177;&#x5907;&#x6307;&#x5357;&#x4E2D;&#x63D0;&#x5230;&#x7684; OTP &#x548C; Elixir &#x7684;&#x76F8;&#x540C;&#x7248;&#x672C;&#x3002; &#x4E0D;&#x4F7F;&#x7528;&#x6B63;&#x786E;&#x7684;&#x7248;&#x672C;&#x4F1A;&#x5BFC;&#x81F4;&#x5B89;&#x88C5;&#x65F6;&#x51FA;&#x73B0;&#x95EE;&#x9898;&#x3002; &#x5728;&#x64B0;&#x5199;&#x672C;&#x6587;&#x65F6;&#xFF0C;&#x4EFB;&#x4F55; Elixir &#x7248;&#x672C;&#xFF08;&#x4F7F;&#x7528; Erlang/OTP 21&#x7F16;&#x8BD1;&#xFF09;&#x90FD;&#x5E94;&#x8BE5;&#x53EF;&#x4EE5;&#x5DE5;&#x4F5C;&#x3002;</p>
<p>&#x73AF;&#x5883;&#x5B89;&#x88C5;&#x5B8C;&#x6210;&#x540E;&#xFF0C;&#x60A8;&#x5E94;&#x5F53;&#x5DF2;&#x7ECF;&#x51C6;&#x5907;&#x597D;&#x6784;&#x5EFA;&#x60A8;&#x7684;&#x7B2C;&#x4E00;&#x4E2A; Nerves &#x9879;&#x76EE;&#x4E86;&#xFF01;</p>
<p>&#x6211;&#x4EEC;&#x7684;&#x76EE;&#x6807;&#x662F;&#x5D4C;&#x5165;&#x5F0F;&#x5F00;&#x53D1;&#x4E2D;&#x7684; &#x201C;Hello world&#x201D; &#xFF1A;&#x901A;&#x8FC7;&#x8C03;&#x7528;&#x7B80;&#x5355;&#x7684; HTTP API &#x6765;&#x63A7;&#x5236;&#x95EA;&#x70C1;&#x7684; LED&#x3002;</p>
<h2 id="&#x521B;&#x5EFA;&#x9879;&#x76EE;">&#x521B;&#x5EFA;&#x9879;&#x76EE;</h2>
<p>&#x8981;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x9879;&#x76EE;&#xFF0C;&#x8BF7;&#x8FD0;&#x884C; <code>mix nerves.new network_led</code> &#x5E76;&#x5728;&#x63D0;&#x793A;&#x662F;&#x5426;&#x83B7;&#x53D6;&#x548C;&#x5B89;&#x88C5;&#x4F9D;&#x8D56;&#x9879;&#x65F6;&#x8F93;&#x5165; <code>Y</code>&#x3002;</p>
<p>&#x4F60;&#x5E94;&#x8BE5;&#x4F1A;&#x770B;&#x5230;&#x4EE5;&#x4E0B;&#x8F93;&#x51FA;&#xFF1A;</p>
<pre><code>Your Nerves project was created successfully.

You should now pick a target. See https://hexdocs.pm/nerves/targets.html#content
for supported targets. If your target is on the list, set `MIX_TARGET`
to its tag name:

For example, for the Raspberry Pi 3 you can either
  $ export MIX_TARGET=rpi3
Or prefix `mix` commands like the following:
  $ MIX_TARGET=rpi3 mix firmware

If you will be using a custom system, update the `mix.exs`
dependencies to point to desired system&apos;s package.

Now download the dependencies and build a firmware archive:
  $ cd network_led
  $ mix deps.get
  $ mix firmware

If your target boots up using an SDCard (like the Raspberry Pi 3),
then insert an SDCard into a reader on your computer and run:
  $ mix firmware.burn

Plug the SDCard into the target and power it up. See target documentation
above for more information and other targets.
</code></pre><p>&#x6211;&#x4EEC;&#x7684;&#x9879;&#x76EE;&#x5DF2;&#x7ECF;&#x521B;&#x5EFA;&#x5B8C;&#x6210;&#xFF0C;&#x5E76;&#x51C6;&#x5907;&#x597D;&#x88AB;&#x5199;&#x5165;&#x5230;&#x6211;&#x4EEC;&#x6D4B;&#x8BD5;&#x8BBE;&#x5907;&#x4E2D;&#xFF01; &#x6211;&#x4EEC;&#x73B0;&#x5728;&#x5F00;&#x59CB;&#x8BD5;&#x8BD5;&#x5427;&#xFF01;</p>
<p>&#x5BF9;&#x4E8E; Raspberry Pi 3&#xFF0C;&#x60A8;&#x53EF;&#x4EE5;&#x8BBE;&#x7F6E; <code>MIX_TARGET=rpi3</code>&#xFF0C;&#x4F46;&#x60A8;&#x53EF;&#x4EE5;&#x6839;&#x636E;&#x76EE;&#x6807;&#x786C;&#x4EF6;&#x66F4;&#x6539;&#x6B64;&#x8BBE;&#x7F6E;&#x4EE5;&#x9002;&#x5E94;&#x60A8;&#x7684;&#x786C;&#x4EF6;&#xFF08;&#x8BF7;&#x53C2;&#x9605; <a href="https://hexdocs.pm/nerves/targets.html#content" target="_blank">Nerves &#x6587;&#x6863;</a>).</p>
<p>&#x8BA9;&#x6211;&#x4EEC;&#x5148;&#x8BBE;&#x7F6E;&#x6211;&#x4EEC;&#x7684;&#x4F9D;&#x8D56;&#x9879;&#xFF1A;</p>
<pre><code>$ export MIX_TARGET=rpi3
$ cd network_led
$ mix deps.get

....

Nerves environment
  MIX_TARGET:   rpi3
  MIX_ENV:      dev
Resolving Nerves artifacts...
  Resolving nerves_system_rpi3
  =&gt; Trying https://github.com/nerves-project/nerves_system_rpi3/releases/download/v1.7.0/nerves_system_rpi3-portable-1.7.0-17EA89A.tar.gz
|==================================================| 100% (133 / 133) MB
  =&gt; Success
  Resolving nerves_toolchain_arm_unknown_linux_gnueabihf
  =&gt; Trying https://github.com/nerves-project/toolchains/releases/download/v1.1.0/nerves_toolchain_arm_unknown_linux_gnueabihf-darwin_x86_64-1.1.0-2305AD8.tar.xz
|==================================================| 100% (50 / 50) MB
  =&gt; Success
</code></pre><p>&#x6CE8;&#x610F;&#xFF1A;&#x786E;&#x4FDD;&#x5728;&#x8FD0;&#x884C; <code>mix deps.get</code> &#x4E4B;&#x524D;&#x8BBE;&#x7F6E;&#x4E86;&#x6307;&#x5B9A;&#x76EE;&#x6807;&#x8BBE;&#x5907;&#x7684;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#xFF0C;&#x56E0;&#x4E3A;&#x5B83;&#x5C06;&#x4E3A;&#x6307;&#x5B9A;&#x8BBE;&#x5907;&#x4E0B;&#x8F7D;&#x76F8;&#x5E94;&#x7684;&#x7CFB;&#x7EDF;&#x955C;&#x50CF;&#x548C;&#x5DE5;&#x5177;&#x94FE;&#x3002;</p>
<h2 id="&#x70E7;&#x5F55;&#x56FA;&#x4EF6;">&#x70E7;&#x5F55;&#x56FA;&#x4EF6;</h2>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x95EA;&#x5B58;&#x9A71;&#x52A8;&#x5668;&#x4E86;&#x3002; &#x5C06;&#x5361;&#x653E;&#x5165;&#x8BFB;&#x5361;&#x5668;&#xFF0C;&#x5982;&#x679C;&#x60A8;&#x5728;&#x524D;&#x9762;&#x7684;&#x6B65;&#x9AA4;&#x4E2D;&#x6B63;&#x786E;&#x8BBE;&#x7F6E;&#x4E86;&#x6240;&#x6709;&#x5185;&#x5BB9;&#xFF0C;&#x5728;&#x8FD0;&#x884C; <code>mix firmware.burn</code> &#x5E76;&#x786E;&#x8BA4;&#x8981;&#x4F7F;&#x7528;&#x7684;&#x8BBE;&#x5907;&#x4E4B;&#x540E;&#xFF0C;&#x60A8;&#x5E94;&#x8BE5;&#x5F97;&#x5230;&#x4EE5;&#x4E0B;&#x63D0;&#x793A;&#xFF1A;</p>
<pre><code>Building ......../network_led/_build/rpi_dev/nerves/images/network_led.fw...
Use 7.42 GiB memory card found at /dev/rdisk2? [Yn]
</code></pre><p>&#x5982;&#x679C;&#x4F60;&#x786E;&#x5B9A;&#x8FD9;&#x662F;&#x4F60;&#x60F3;&#x8981;&#x70E7;&#x5F55;&#x7684;&#x5361; - &#x9009;&#x62E9; <code>Y</code>, &#x4E00;&#x6BB5;&#x65F6;&#x95F4;&#x540E;&#x5B58;&#x50A8;&#x5361;&#x5C31;&#x70E7;&#x597D;&#x4E86;&#xFF1A;</p>
<pre><code>Use 7.42 GiB memory card found at /dev/rdisk2? [Yn]
|====================================| 100% (32.51 / 32.51) MB
Success!
Elapsed time: 8.022 s
</code></pre><p>&#x73B0;&#x5728;&#x662F;&#x65F6;&#x5019;&#x5C06;&#x5B58;&#x50A8;&#x5361;&#x653E;&#x5165;&#x60A8;&#x7684;&#x8BBE;&#x5907;&#x5E76;&#x9A8C;&#x8BC1;&#x5B83;&#x662F;&#x5426;&#x6709;&#x6548;&#x3002;</p>
<p>&#x5982;&#x679C;&#x60A8;&#x8FDE;&#x63A5;&#x4E86;&#x4E00;&#x4E2A;&#x5C4F;&#x5E55; - &#x5728;&#x63D2;&#x5165;&#x6B64;&#x5B58;&#x50A8;&#x5361;&#x7684;&#x8BBE;&#x5907;&#x542F;&#x52A8;&#x540E;&#xFF0C;&#x60A8;&#x5E94;&#x8BE5;&#x770B;&#x5230;&#x4E00;&#x4E2A; Linux &#x542F;&#x52A8;&#x5E8F;&#x5217;&#x3002;</p>
<h2 id="&#x7F51;&#x7EDC;&#x8BBE;&#x7F6E;">&#x7F51;&#x7EDC;&#x8BBE;&#x7F6E;</h2>
<p>&#x4E0B;&#x4E00;&#x6B65;&#x662F;&#x8FDE;&#x63A5;&#x7F51;&#x7EDC;&#x3002; Nerves &#x751F;&#x6001;&#x7CFB;&#x7EDF;&#x63D0;&#x4F9B;&#x5404;&#x79CD;&#x8F6F;&#x4EF6;&#x5305;&#xFF0C;<a href="https://github.com/nerves-project/nerves_network" target="_blank">nerves_network</a> &#x662F;&#x6211;&#x4EEC;&#x901A;&#x8FC7;&#x6709;&#x7EBF;&#x4EE5;&#x592A;&#x7F51;&#x7AEF;&#x53E3;&#x5C06;&#x8BBE;&#x5907;&#x8FDE;&#x63A5;&#x5230;&#x7F51;&#x7EDC;&#x6240;&#x9700;&#x7684;&#x3002;</p>
<p>&#x5176;&#x5B9E;&#x5B83;&#x5DF2;&#x4F5C;&#x4E3A; <code>nerves_init_gadget</code> &#x7684;&#x4F9D;&#x8D56;&#x9879;&#x5B58;&#x5728;&#x4E8E;&#x60A8;&#x7684;&#x9879;&#x76EE;&#x4E2D;&#x3002; &#x4F46;&#x662F;&#xFF0C;&#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x5B83;&#x4F7F;&#x7528; DHCP&#xFF08;&#x5728;&#x8FD0;&#x884C; <code>config: nerves_init_gadget</code> &#x4E4B;&#x540E;&#xFF0C;&#x8BF7;&#x53C2;&#x9605; <code>config/config.exs</code> &#x4E2D;&#x7684;&#x914D;&#x7F6E;&#xFF09;&#x3002; &#x62E5;&#x6709;&#x9759;&#x6001; IP &#x5730;&#x5740;&#x66F4;&#x5BB9;&#x6613;&#x3002;</p>
<p>&#x8981;&#x8BBE;&#x7F6E;&#x9759;&#x6001;&#x7F51;&#x7EDC;&#xFF0C;&#x60A8;&#x9700;&#x8981;&#x5728; <code>config/config.exs</code> &#x6587;&#x4EF6;&#x4E2D;&#x6DFB;&#x52A0;&#x4EE5;&#x4E0B;&#x884C;:</p>
<pre><code># Statically assign an address
config :nerves_network, :default,
  eth0: [
    ipv4_address_method: :static,
    ipv4_address: &quot;192.168.88.2&quot;,
    ipv4_subnet_mask: &quot;255.255.255.0&quot;,
    nameservers: [&quot;8.8.8.8&quot;, &quot;8.8.4.4&quot;]
  ]
</code></pre><p>&#x8BF7;&#x6CE8;&#x610F;&#xFF0C;&#x6B64;&#x914D;&#x7F6E;&#x9002;&#x53EA;&#x9002;&#x7528;&#x4E8E;&#x6709;&#x7EBF;&#x8FDE;&#x63A5;&#x3002; &#x5982;&#x679C;&#x8981;&#x4F7F;&#x7528;&#x65E0;&#x7EBF;&#x8FDE;&#x63A5;&#x2014;&#x2014;&#x8BF7;&#x67E5;&#x770B;<a href="https://github.com/nerves-project/nerves_network#wifi-networking" target="_blank">Nerves network documentation</a>.</p>
<p>&#x8BF7;&#x6CE8;&#x610F;&#xFF0C;&#x60A8;&#x9700;&#x8981;&#x6839;&#x636E;&#x6240;&#x5728;&#x7684;&#x5C40;&#x57DF;&#x7F51;&#x7684;&#x5177;&#x4F53;&#x60C5;&#x51B5;&#x8BBE;&#x7F6E;&#x8FD9;&#x91CC;&#x7684;&#x53C2;&#x6570;&#xFF0C;&#x5728;&#x6211;&#x7684;&#x7F51;&#x7EDC;&#x4E2D;&#x6709;&#x4E00;&#x4E2A;&#x672A;&#x5206;&#x914D;&#x7684; IP <code>192.168.88.2</code>&#xFF0C;&#x6240;&#x4EE5;&#x4F7F;&#x7528;&#x4E86;&#x8FD9;&#x4E2A; IP&#x3002; &#x4F46;&#x662F;&#x5728;&#x60A8;&#x7684;&#x7F51;&#x7EDC;&#x73AF;&#x5883;&#x4E2D;&#xFF0C;&#x8FD9;&#x4E9B;&#x53C2;&#x6570;&#x53EF;&#x80FD;&#x4F1A;&#x6709;&#x6240;&#x4E0D;&#x540C;&#x3002;</p>
<p>&#x66F4;&#x6539;&#x540E;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x8FD0;&#x884C; <code>mix firmware.burn</code> &#x91CD;&#x65B0;&#x70E7;&#x5F55;&#x56FA;&#x4EF6;&#x7684;&#x66F4;&#x6539;&#x7248;&#x672C;&#xFF0C;&#x7136;&#x540E;&#x4F7F;&#x7528;&#x65B0;&#x5361;&#x542F;&#x52A8;&#x8BBE;&#x5907;&#x3002;</p>
<p>&#x5F53;&#x60A8;&#x6253;&#x5F00;&#x8BBE;&#x5907;&#x7535;&#x6E90;&#x540E;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>ping</code> &#x547D;&#x4EE4;&#x67E5;&#x770B;&#x5B83;&#x662F;&#x5426;&#x5728;&#x7EBF;&#x3002;</p>
<pre><code>Request timeout for icmp_seq 206
Request timeout for icmp_seq 207
64 bytes from 192.168.88.2: icmp_seq=208 ttl=64 time=2.247 ms
64 bytes from 192.168.88.2: icmp_seq=209 ttl=64 time=2.658 ms
</code></pre><p>&#x6B64;&#x8F93;&#x51FA;&#x8868;&#x793A;&#x8BBE;&#x5907;&#x5DF2;&#x7ECF;&#x8FDE;&#x4E0A;&#x7F51;&#xFF0C;&#x5E76;&#x4E14;&#x53EF;&#x4EE5;&#x88AB;&#x8BBF;&#x95EE;&#x5230;&#x4E86;</p>
<h2 id="&#x8FDC;&#x7A0B;&#x66F4;&#x65B0;">&#x8FDC;&#x7A0B;&#x66F4;&#x65B0;</h2>
<p>&#x5230;&#x76EE;&#x524D;&#x4E3A;&#x6B62;&#xFF0C;&#x6211;&#x4EEC;&#x4E00;&#x76F4;&#x5728;&#x70E7;&#x5F55; SD &#x5361;&#x5E76;&#x5C06;&#x5B83;&#x4EEC;&#x901A;&#x8FC7;&#x7269;&#x7406;&#x65B9;&#x5F0F;&#x52A0;&#x8F7D;&#x5230;&#x6211;&#x4EEC;&#x7684;&#x786C;&#x4EF6;&#x4E2D;&#x3002; &#x867D;&#x7136;&#x8FD9;&#x5F88;&#x597D;&#xFF0C;&#x4F46;&#x901A;&#x8FC7;&#x7F51;&#x7EDC;&#x63A8;&#x9001;&#x6211;&#x4EEC;&#x7684;&#x66F4;&#x65B0;&#x66F4;&#x4E3A;&#x76F4;&#x63A5;&#x3002; <code>nerves_firmware_ssh</code> &#x5305;&#x6B63;&#x662F;&#x505A;&#x8FD9;&#x4E2A;&#x4E8B;&#x7684;&#x3002; &#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x5B83;&#x5DF2;&#x5B58;&#x5728;&#x4E8E;&#x60A8;&#x7684;&#x9879;&#x76EE;&#x4E2D;&#xFF0C;&#x5E76;&#x914D;&#x7F6E;&#x4E3A;&#x81EA;&#x52A8;&#x68C0;&#x6D4B;&#x5E76;&#x5728;&#x76EE;&#x5F55;&#x4E2D;&#x67E5;&#x627E; SSH &#x5BC6;&#x94A5;&#x3002;</p>
<p>&#x8981;&#x4F7F;&#x7528;&#x7F51;&#x7EDC;&#x56FA;&#x4EF6;&#x66F4;&#x65B0;&#x529F;&#x80FD;&#xFF0C;&#x60A8;&#x9700;&#x8981;&#x901A;&#x8FC7; <code>mix firmware.gen.script</code> &#x751F;&#x6210;&#x4E0A;&#x4F20;&#x811A;&#x672C;&#x3002; &#x8BE5;&#x547D;&#x4EE4;&#x5C06;&#x751F;&#x6210;&#x4E00;&#x4E2A;&#x65B0;&#x7684; <code>upload.sh</code> &#x811A;&#x672C;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x8FD0;&#x884C;&#x8BE5;&#x811A;&#x672C;&#x6765;&#x66F4;&#x65B0;&#x56FA;&#x4EF6;&#x3002;</p>
<p>&#x5982;&#x679C;&#x5728;&#x4E0A;&#x4E00;&#x6B65;&#x4E4B;&#x540E;&#x7F51;&#x7EDC;&#x6B63;&#x5E38;&#x8FD0;&#x884C;&#xFF0C;&#x60A8;&#x5C31;&#x53EF;&#x4EE5;&#x5F00;&#x59CB;&#x4F7F;&#x7528;&#x4E86;&#x3002;</p>
<p>&#x8981;&#x66F4;&#x65B0;&#x60A8;&#x7684;&#x8BBE;&#x7F6E;&#xFF0C;&#x6700;&#x7B80;&#x5355;&#x7684;&#x65B9;&#x6CD5;&#x662F;&#x4F7F;&#x7528; <code>mix firmware &amp;&amp; ./upload.sh 192.168.88.2</code>&#xFF1A;&#x7B2C;&#x4E00;&#x4E2A;&#x547D;&#x4EE4;&#x521B;&#x5EFA;&#x66F4;&#x65B0;&#x7684;&#x56FA;&#x4EF6;&#xFF0C;&#x7B2C;&#x4E8C;&#x4E2A;&#x547D;&#x4EE4;&#x901A;&#x8FC7;&#x7F51;&#x7EDC;&#x63A8;&#x9001;&#x5B83;&#x5E76;&#x91CD;&#x65B0;&#x542F;&#x52A8;&#x8BBE;&#x5907;&#x3002; &#x60A8;&#x6700;&#x7EC8;&#x53EF;&#x4EE5;&#x505C;&#x6B62;&#x5C06; SD &#x5361;&#x63D2;&#x5165;&#x548C;&#x62D4;&#x51FA;&#x8BBE;&#x5907;&#xFF01;</p>
<p><em>&#x63D0;&#x793A;&#xFF1A;<code>ssh 192.168.88.2</code> &#x5728;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x7684;&#x4E0A;&#x4E0B;&#x6587;&#x4E2D;&#x4E3A;&#x60A8;&#x63D0;&#x4F9B;&#x8BBE;&#x5907;&#x4E0A;&#x7684; IEx shell&#x3002;</em></p>
<p><em>&#x6545;&#x969C;&#x6392;&#x9664;&#xFF1A;&#x5982;&#x679C;&#x60A8;&#x7684;&#x4E3B;&#x6587;&#x4EF6;&#x5939;&#x4E2D;&#x6CA1;&#x6709;&#x73B0;&#x6709;&#x7684; ssh &#x5BC6;&#x94A5;&#xFF0C;&#x5219;&#x4F1A;&#x51FA;&#x73B0;&#x9519;&#x8BEF; <code>No SSH public keys found in ~/.ssh.</code> &#x5728;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x60A8;&#x9700;&#x8981;&#x8FD0;&#x884C; <code>ssh-keygen</code> &#x5E76;&#x91CD;&#x65B0;&#x70E7;&#x5F55;&#x56FA;&#x4EF6;&#x4EE5;&#x4F7F;&#x7528;&#x7F51;&#x7EDC;&#x66F4;&#x65B0;&#x529F;&#x80FD;.</em></p>
<h2 id="&#x63A7;&#x5236;-led">&#x63A7;&#x5236; LED</h2>
<p>&#x8981;&#x4E0E; LED &#x4EA4;&#x4E92;&#xFF0C;&#x9700;&#x8981;&#x5B89;&#x88C5; <a href="https://github.com/nerves-project/nerves_leds" target="_blank">nerves_leds</a> &#x8F6F;&#x4EF6;&#x5305;&#xFF0C;&#x8FD9;&#x9700;&#x8981;&#x6DFB;&#x52A0; <code>{:nerves_leds&#xFF0C;&quot;&#x301C;&gt; 0.8&quot;, targets: @all_targets},</code> &#x5230; <code>mix.exs</code> &#x6587;&#x4EF6;&#x3002;</p>
<p>&#x8BBE;&#x7F6E;&#x4F9D;&#x8D56;&#x5173;&#x7CFB;&#x540E;&#xFF0C;&#x9700;&#x8981;&#x4E3A;&#x8BBE;&#x5907;&#x914D;&#x7F6E; LED &#x5217;&#x8868;&#x3002; &#x4F8B;&#x5982;&#xFF0C;&#x5BF9;&#x4E8E;&#x6240;&#x6709; Raspberry Pi &#x578B;&#x53F7;&#xFF0C;&#x677F;&#x8F7D;&#x53EA;&#x6709;&#x4E00;&#x4E2A; LED&#xFF1A;<code>led0</code>&#x3002; &#x8BA9;&#x6211;&#x4EEC;&#x901A;&#x8FC7;&#x5728; <code>config/config.exs</code> &#x4E2D;&#x6DFB;&#x52A0;&#x4E00;&#x884C; <code>config: nerves_leds, names:[green: &quot;led0&quot;]</code> &#x6765;&#x4F7F;&#x7528;&#x5B83;&#x3002;</p>
<p>&#x5BF9;&#x4E8E;&#x5176;&#x4ED6;&#x8BBE;&#x5907;&#xFF0C;&#x60A8;&#x53EF;&#x4EE5;&#x67E5;&#x770B;<a href="https://github.com/nerves-project/nerves_examples/tree/master/hello_leds/config" target="_blank">nerves_examples &#x9879;&#x76EE;&#x7684;&#x76F8;&#x5E94;&#x90E8;&#x5206;</a>.</p>
<p>&#x914D;&#x7F6E; LED &#x540E;&#xFF0C;&#x6211;&#x4EEC;&#x80AF;&#x5B9A;&#x9700;&#x8981;&#x4EE5;&#x67D0;&#x79CD;&#x65B9;&#x5F0F;&#x63A7;&#x5236;&#x5B83;&#x3002; &#x4E3A;&#x6B64;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x5728; <code>lib/network_led/blinker.ex</code> &#x4E2D;&#x6DFB;&#x52A0;&#x4E00;&#x4E2A; GenServer&#xFF08;&#x8BF7;&#x53C2;&#x9605; <a href="../../advanced/otp-concurrency">OTP Concurrency</a> &#x8BFE;&#x7A0B;&#x4E2D;&#x6709;&#x5173; GenServers &#x7684;&#x8BE6;&#x7EC6;&#x4FE1;&#x606F;&#xFF09;&#xFF0C;&#x5176;&#x4E2D;&#x5305;&#x542B;&#x4EE5;&#x4E0B;&#x5185;&#x5BB9;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">NetworkLed</span></span>.Blinker <span class="hljs-keyword">do</span>
  <span class="hljs-keyword">use</span> GenServer

  <span class="hljs-variable">@moduledoc</span> <span class="hljs-string">&quot;&quot;</span><span class="hljs-string">&quot;
    Simple GenServer to control GPIO #18.
  &quot;</span><span class="hljs-string">&quot;&quot;</span>

  require Logger
  <span class="hljs-keyword">alias</span> Nerves.Leds

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">start_link</span></span>(state \\ []) <span class="hljs-keyword">do</span>
    GenServer.start_link(__MODULE_<span class="hljs-number">_</span>, state, <span class="hljs-symbol">name:</span> __MODULE_<span class="hljs-number">_</span>)
  <span class="hljs-keyword">end</span>

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">init</span></span>(state) <span class="hljs-keyword">do</span>
    enable()

    {<span class="hljs-symbol">:ok</span>, state}
  <span class="hljs-keyword">end</span>

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">handle_cast</span></span>(<span class="hljs-symbol">:enable</span>, state) <span class="hljs-keyword">do</span>
    Logger.info(<span class="hljs-string">&quot;Enabling LED&quot;</span>)
    Leds.set(<span class="hljs-symbol">green:</span> <span class="hljs-keyword">true</span>)

    {<span class="hljs-symbol">:noreply</span>, state}
  <span class="hljs-keyword">end</span>

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">handle_cast</span></span>(<span class="hljs-symbol">:disable</span>, state) <span class="hljs-keyword">do</span>
    Logger.info(<span class="hljs-string">&quot;Disabling LED&quot;</span>)
    Leds.set(<span class="hljs-symbol">green:</span> <span class="hljs-keyword">false</span>)

    {<span class="hljs-symbol">:noreply</span>, state}
  <span class="hljs-keyword">end</span>

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">enable</span></span>() <span class="hljs-keyword">do</span>
    GenServer.cast(__MODULE_<span class="hljs-number">_</span>, <span class="hljs-symbol">:enable</span>)
  <span class="hljs-keyword">end</span>

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">disable</span></span>() <span class="hljs-keyword">do</span>
    GenServer.cast(__MODULE_<span class="hljs-number">_</span>, <span class="hljs-symbol">:disable</span>)
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x8981;&#x542F;&#x7528;&#x6B64;&#x529F;&#x80FD;&#xFF0C;&#x8FD8;&#x9700;&#x8981;&#x5C06;&#x5176;&#x6DFB;&#x52A0;&#x5230; <code>lib/network_led/application.ex</code> &#x4E2D;&#x7684;&#x76D1;&#x7763;&#x6811;&#x4E2D;&#xFF1A;&#x5728; <code>def children&#xFF08;_target&#xFF09;do</code> &#x7EC4;&#x4E0B;&#x6DFB;&#x52A0; <code>{NetworkLed.Blinker, name: NetworkLed.Blinker}</code>&#x3002;</p>
<p>&#x8BF7;&#x6CE8;&#x610F;&#xFF0C;Nerves &#x5728;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x4E2D;&#x6709;&#x4E24;&#x4E2A;&#x4E0D;&#x540C;&#x7684;&#x76D1;&#x7763;&#x6811; - &#x4E00;&#x4E2A;&#x7528;&#x4E8E;&#x4E3B;&#x673A;&#xFF0C;&#x4E00;&#x4E2A;&#x7528;&#x4E8E;&#x5B9E;&#x9645;&#x8BBE;&#x5907;&#x3002;</p>
<p>&#x5728;&#x6B64;&#x4E4B;&#x540E; - &#x5C31;&#x662F;&#x8FD9;&#x6837;&#xFF01; &#x60A8;&#x5B9E;&#x9645;&#x4E0A;&#x53EF;&#x4EE5;&#x4E0A;&#x4F20;&#x56FA;&#x4EF6;&#x5E76;&#x901A;&#x8FC7;&#x76EE;&#x6807;&#x8BBE;&#x5907;&#x4E0A;&#x7684; ssh &#x8FD0;&#x884C; IEx &#x68C0;&#x67E5; <code>NetworkLed.Blinker.disable()</code> &#x5173;&#x95ED; LED&#xFF08;&#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B;&#x5728;&#x4EE3;&#x7801;&#x4E2D;&#x542F;&#x7528;&#xFF09;&#xFF0C;&#x4EE5;&#x53CA; <code>NetworkLed.Blinker.enable()</code> &#x6253;&#x5F00;&#x5B83;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4ECE;&#x547D;&#x4EE4;&#x63D0;&#x793A;&#x7B26;&#x63A7;&#x5236; LED &#x4E86;&#xFF01;</p>
<p>&#x73B0;&#x5728;&#xFF0C;&#x5269;&#x4E0B;&#x7684;&#x552F;&#x4E00;&#x7F3A;&#x5931;&#x7684;&#x90E8;&#x5206;&#x662F;&#x901A;&#x8FC7;&#x7F51;&#x7EDC;&#x754C;&#x9762;&#x63A7;&#x5236; LED&#x3002;</p>
<h2 id="&#x6DFB;&#x52A0;-web-&#x670D;&#x52A1;&#x5668;">&#x6DFB;&#x52A0; Web &#x670D;&#x52A1;&#x5668;</h2>
<p>&#x5728;&#x8FD9;&#x4E00;&#x6B65;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x4F7F;&#x7528; <code>Plug.Router</code>&#x3002; &#x5982;&#x679C;&#x60A8;&#x9700;&#x8981;&#x63D0;&#x9192; - &#x8BF7;&#x968F;&#x610F;&#x6D4F;&#x89C8;<a href="../../../lessons/specifics/plug">&#x63D2;&#x4EF6;</a> &#x8BFE;&#x7A0B;.</p>
<p>&#x9996;&#x5148;&#xFF0C;&#x6211;&#x4EEC;&#x5C06; <code>{plug_cowboy, &apos;&#x301C;&gt; 2.0&#x201C;}&#xFF0C;</code> &#x6DFB;&#x52A0;&#x5230; <code>mix.exs</code> &#x5E76;&#x5B89;&#x88C5;&#x4F9D;&#x8D56;&#x9879;&#x3002;</p>
<p>&#x7136;&#x540E;&#xFF0C;&#x5728; <code>lib/network_led/http.ex</code> &#x4E2D;&#x6DFB;&#x52A0;&#x5B9E;&#x9645;&#x8FDB;&#x7A0B;&#x6765;&#x5904;&#x7406;&#x8FD9;&#x4E9B;&#x8BF7;&#x6C42;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">NetworkLed</span></span>.Http <span class="hljs-keyword">do</span>
  <span class="hljs-keyword">use</span> Plug.Router

  plug(<span class="hljs-symbol">:match</span>)
  plug(<span class="hljs-symbol">:dispatch</span>)

  get(<span class="hljs-string">&quot;/&quot;</span>, <span class="hljs-symbol">do:</span> send_resp(conn, <span class="hljs-number">200</span>, <span class="hljs-string">&quot;Feel free to use API endpoints!&quot;</span>))

  get <span class="hljs-string">&quot;/enable&quot;</span> <span class="hljs-keyword">do</span>
    NetworkLed.Blinker.enable()
    send_resp(conn, <span class="hljs-number">200</span>, <span class="hljs-string">&quot;LED enabled&quot;</span>)
  <span class="hljs-keyword">end</span>

  get <span class="hljs-string">&quot;/disable&quot;</span> <span class="hljs-keyword">do</span>
    NetworkLed.Blinker.disable()
    send_resp(conn, <span class="hljs-number">200</span>, <span class="hljs-string">&quot;LED disabled&quot;</span>)
  <span class="hljs-keyword">end</span>

  match(<span class="hljs-number">_</span>, <span class="hljs-symbol">do:</span> send_resp(conn, <span class="hljs-number">404</span>, <span class="hljs-string">&quot;Oops!&quot;</span>))
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x6700;&#x540E;&#x4E00;&#x6B65; - &#x5C06; <code>{Plug.Cowboy, scheme: :http, plug: NetworkLed.Http, options: [port: 80]}</code> &#x6DFB;&#x52A0;&#x5230;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x76D1;&#x7763;&#x6811;&#x4E2D;&#x3002;</p>
<p>&#x56FA;&#x4EF6;&#x66F4;&#x65B0;&#x540E;&#xFF0C;&#x60A8;&#x53EF;&#x4EE5;&#x8BD5;&#x8BD5;&#x3002; &#x8BBF;&#x95EE; <code>http://192.168.88.2/</code> &#x5C06;&#x8FD4;&#x56DE;&#x7EAF;&#x6587;&#x672C;&#x54CD;&#x5E94;&#xFF0C;<code>http://192.168.88.2/enable</code> &#x548C; <code>http://192.168.88.2/disable</code> &#x5C06;&#x63A7;&#x5236;&#x7981;&#x7528;&#x5E76;&#x542F;&#x7528;&#x8BE5; LED&#xFF01;</p>
<p>&#x60A8;&#x751A;&#x81F3;&#x53EF;&#x4EE5;&#x5C06; Phoenix &#x652F;&#x6301;&#x7684;&#x7528;&#x6237;&#x754C;&#x9762;&#x6253;&#x5305;&#x5230;&#x60A8;&#x7684; Nerves &#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x4E2D;&#xFF0C;&#x4F46;&#x662F;&#xFF0C;&#x8FD9;<a href="https://github.com/nerves-project/nerves/blob/master/docs/User%20Interfaces.md#phoenix-web-interfaces" target="_blank">&#x9700;&#x8981;&#x8FDB;&#x884C;&#x4E00;&#x4E9B;&#x8C03;&#x6574;</a>&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="protocols.html" class="navigation navigation-prev " aria-label="Previous page: 协议">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../ecto/basics.html" class="navigation navigation-next " aria-label="Next page: Basics">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"version":"1.0.0","title":"Nerves","level":"2.14","depth":1,"next":{"title":"Basics","level":"3.1","depth":1,"path":"lessons/ecto/basics.md","ref":"lessons/ecto/basics.md","articles":[]},"previous":{"title":"协议","level":"2.13","depth":1,"path":"lessons/advanced/protocols.md","ref":"lessons/advanced/protocols.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"lessons/advanced/nerves.md","mtime":"2019-11-05T02:25:51.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-11-05T03:01:22.004Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

